package com.gitee.fantaros.web.fan.netcut.service;

import com.gitee.fantaros.web.fan.netcut.bean.PasterInfo;
import com.gitee.fantaros.web.fan.netcut.mapper.PasterMapper;
import com.gitee.fantaros.web.fan.netcut.util.WhisperUtil;
import com.vaadin.flow.spring.annotation.SpringComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SpringComponent
@Service
@SuppressWarnings("unused")
public class PasterService {
    private final PasterMapper pasterMapper;

    public PasterService(@Autowired PasterMapper pasterMapper) {
        this.pasterMapper = pasterMapper;
    }

    public void prepareDatabaseTable() {
        pasterMapper.createPasterInfoTable();
    }

    public PasterInfo queryInfoById(String pasterName) {
        PasterInfo pasterInfo = pasterMapper.queryInfoById(pasterName);
        return pasterInfo;
//        return decodeContent(pasterInfo);
    }

//    private PasterInfo decodeContent(PasterInfo pasterInfo) {
//        if (pasterInfo != null && pasterInfo.getContent() != null && pasterInfo.getContent().length() > 4) {
//            String content = WhisperUtil.decodeString(pasterInfo.getContent());
//            PasterInfo nPasterInfo = PasterInfo.copyOf(pasterInfo);
//            nPasterInfo.setContent(content);
//            return nPasterInfo;
//        }
//        return pasterInfo;
//    }

    public boolean checkExists(String pasterName) {
        int c = pasterMapper.checkExists(pasterName);
        return c > 0;
    }

    public boolean checkPasswd(String pasterName, String passwd) {
        String encodedPasswd = WhisperUtil.encodeString(passwd);
        return checkEncodedPasswd(pasterName, encodedPasswd);
    }

    public boolean checkEncodedPasswd(String pasterName, String encodedPasswd) {
        int c = pasterMapper.checkPasswd(pasterName, encodedPasswd);
        return c > 0;
    }

    public void insertOne(PasterInfo pasterInfo) {
        pasterMapper.insertOne(pasterInfo);
    }

    public void updateContent(PasterInfo pasterInfo) {
//        PasterInfo p = encodeContent(pasterInfo);
        pasterMapper.updateContent(pasterInfo                                   );
    }

//    private PasterInfo encodeContent(PasterInfo pasterInfo) {
//        if (pasterInfo != null && pasterInfo.getContent() != null && !pasterInfo.getContent().isEmpty()) {
//
//            String content = WhisperUtil.encodeString(pasterInfo.getContent());
//            PasterInfo nPasterInfo = PasterInfo.copyOf(pasterInfo);
//            nPasterInfo.setContent(content);
//            return nPasterInfo;
//        }
//        return pasterInfo;
//    }

}
